/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.adou.common.util.weixin;
 
 
import com.adou.common.util.Aes.AesException;
import com.adou.common.util.Aes.WXBizMsgCrypt;

import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * 核心请求处理类
 * 
 * @author heqian
 * 
 */
public class CoreServlet extends HttpServlet {
    String sToken = "987d114fee53";//这个Token是随机生成，但是必须跟企业号上的相同
    String sCorpID = "wx8ccf4a324673002a";//这里是你企业号的CorpID(AppID)
    /**
     * 确认请求来自微信服务器
     * @throws IOException 
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
        // 微信加密签名 
        String sVerifyMsgSig = request.getParameter("signature");
        // 时间戳
        String sVerifyTimeStamp = request.getParameter("timestamp");
        // 随机数
        String sVerifyNonce = request.getParameter("nonce");
        // 随机字符串
        String sVerifyEchoStr = request.getParameter("echostr");
        
        PrintWriter out = response.getWriter();  
        WXBizMsgCrypt wxcpt;
        try {
            wxcpt = new WXBizMsgCrypt(sToken, sCorpID);
            String sEchoStr = wxcpt.verifyUrl(sVerifyMsgSig, sVerifyTimeStamp,sVerifyNonce, sVerifyEchoStr);
            // 验证URL成功，将sEchoStr返回
            out.print(sEchoStr);  
        } catch (AesException e1) {
            e1.printStackTrace();
        }
    }
 
    /**
     * 处理微信服务器发来的消息
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO 消息的接收、处理、响应
    }
}